<?php

/**
 * sfWidgetFormPropelCountrySelect.
 *
 * @package    scart
 * @subpackage actions
 * @author     Pedro Nilton Olivera Chamochumbi <poliverach@gmail.com>
 */

class sfWidgetFormPropelCountrySelect extends sfWidgetFormPropelSelect
{
  protected function configure($options = array(), $attributes = array())
  {
  	parent::configure();
  	
  	$this->setOption('model', 'Country');
  }
  public function getChoices()
  {
    $choices = array();
    if (false !== $this->getOption('add_empty'))
    {
      $choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
    }

    $method = $this->getOption('method');
    if (!method_exists($this->getOption('model'), $method))
    {
      throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $method, __CLASS__));
    }
    
    
    $criteria = is_null($this->getOption('criteria')) ? new Criteria() : $this->getOption('criteria');
    
    $c = clone $criteria;
    $c->add(CountryPeer::STATUS, '1');
    $c->add(CountryPeer::BY_DEFAULT, '1');
    $c->addAscendingOrderByColumn(CountryPeer::ORDEN);
    
    $countries = CountryPeer::doSelect($c);
    foreach ($countries as $country)
    {
      $choices['----'][$country->getPrimaryKey()] = $country->$method();
    }
    
    $c = clone $criteria;
    $c->add(CountryPeer::STATUS, '1');
    // $c->add(CountryPeer::BY_DEFAULT, '0');
    $c->addAscendingOrderByColumn(CountryPeer::NAME);
    
    $countries = CountryPeer::doSelect($c);
    foreach ($countries as $country)
    {
      $choices['--------'][$country->getPrimaryKey()] = $country->$method();
    }

    return $choices;
  }
}
